//
//  TDCarSourceListHeadView.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/13.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDCarSourceListHeadView.h"
#import "TDCarSourceHeadViewCollecCell.h"
#import "UILabel+height.h"
#import "NSString+Rect.h"
#import "UIView+ZLExtension.h"

@implementation TDCarSourceListHeadView


+ (instancetype)carSourceListHeadView {
    return [[NSBundle mainBundle] loadNibNamed:@"TDCarSourceListHeadView" owner:nil options:nil].lastObject;
}

// MARK: - 麻蛋  collectionView实现不了 fuck了  卧槽 我屮艸芔茻 白写了半天 麻蛋
- (void)awakeFromNib {
    [super awakeFromNib];
//    [self setCollectionView];
    
}


- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    CGFloat totalWidth = 10;
    [_scrolView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

    for (int i = 0; i < self.titleArray.count; i++) {
        NSString *title = self.titleArray[i];
        UILabel *lbe = [[UILabel alloc] init];
        lbe.textAlignment = NSTextAlignmentCenter;
        lbe.text = title;
        lbe.font = [UIFont boldSystemFontOfSize:12];
        lbe.textColor = [UIColor darkTextColor];
        lbe.layer.cornerRadius = 4;
        lbe.layer.borderWidth = 1;
        lbe.layer.borderColor = [UIColor lightGrayColor].CGColor;
        lbe.font = [UIFont boldSystemFontOfSize:12];
        if ([title isEqualToString:@"车型"] || [title isEqualToString:@"日期"] || [title isEqualToString:@"门店"]) {
            lbe.font = [UIFont boldSystemFontOfSize:12];
            lbe.textColor = [UIColor darkTextColor];
            lbe.backgroundColor = [UIColor whiteColor];
        }else {
            lbe.font = [UIFont boldSystemFontOfSize:12];
            lbe.textColor = [UIColor whiteColor];
            lbe.backgroundColor = [UIColor colorWithHex:0xbf0021];
        }
        lbe.clipsToBounds = YES;
        UITapGestureRecognizer *singleCarBrand = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singlCarBrandEvent:)];
        UITapGestureRecognizer *singleDate = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singlDateEvent:)];
        UITapGestureRecognizer *singleStore = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singlStoreEvent:)];
        lbe.tag = i+1;
        lbe.userInteractionEnabled=YES;
        if (lbe.tag == 1) {
            [lbe addGestureRecognizer:singleCarBrand];
        }else if (lbe.tag == 2) {
            [lbe addGestureRecognizer:singleDate];
        }else {
            [lbe addGestureRecognizer:singleStore];
        }
        CGFloat width = [NSString boundingRectWithText:title font:12 size:CGSizeMake(1000, 16)].width;
        lbe.frame = CGRectMake(totalWidth+10, 0, width + 30, 30);
        lbe.zl_centerY = _scrolView.zl_height/2;
        totalWidth += width + 40;
        [_scrolView addSubview:lbe];
    }
    _scrolView.contentSize = CGSizeMake(totalWidth + 10,30);
}

// MARK: - 代表车辆点击
- (void)singlCarBrandEvent:(UITapGestureRecognizer *)tap {
    self.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.userInteractionEnabled = YES;
    });
    if ([self.delegate respondsToSelector:@selector(carSourceListHeadViewBtnClick:)]) {
        [self.delegate carSourceListHeadViewBtnClick:0];
    }
}

// MARK: - 日期选择点击
- (void)singlDateEvent:(UITapGestureRecognizer *)tap {
    self.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.userInteractionEnabled = YES;
    });
    if ([self.delegate respondsToSelector:@selector(carSourceListHeadViewBtnClick:)]) {
        [self.delegate carSourceListHeadViewBtnClick:1];
    }
}

// MARK: - 门店选择点击
- (void)singlStoreEvent:(UITapGestureRecognizer *)tap {
    self.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.userInteractionEnabled = YES;
    });
    if ([self.delegate respondsToSelector:@selector(carSourceListHeadViewBtnClick:)]) {
        [self.delegate carSourceListHeadViewBtnClick:2];
    }
}

- (void)setCollectionView {
    self.titleArray = @[@"车型",@"车辆详细车辆详细车辆详细车辆详细cheliangchelaing",@"门店"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"TDCarSourceHeadViewCollecCell" bundle:nil] forCellWithReuseIdentifier:@"TDCarSourceHeadViewCollecCellId"];
    self.flowLayout.estimatedItemSize = CGSizeMake(60, 34);
    // 1.设置 最小行间距
    self.flowLayout.minimumLineSpacing = 10;
    // 2.设置 最小列间距
    self.flowLayout.minimumInteritemSpacing = 10;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0,20,10,20);
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}


// MARK: - collectionVIew代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TDCarSourceHeadViewCollecCell *innerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TDCarSourceHeadViewCollecCellId" forIndexPath:indexPath];
    innerCell.lbe_historyText.text = self.titleArray[indexPath.item];
    return innerCell;
}

@end
